package com.gtmonitor.processes;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.View;

import com.gtmonitor.GTmonitorService;
import com.gtmonitor.graphics.GraphicsActivity;

public class ProcessView extends View {

    public static int MAXSIZE = 0;
    public static byte[] CPUUsageArray = null;
    public static byte[] CPUSelfArray = null;
    public static int index = 0;
    private static int viewWidth = 0;
    private static int viewHeight = 0;
    private static int viewBottom = 0;
    private static int graphHeight = 0;
    private static int graphBottom = 0;
    private static final int textHeight = 15;
    private static Paint paintCPU = new Paint();
    private static Paint paintSelfCPU = new Paint();
    private static int CPUred = 255;
    private static int CPUgreen = 50;
    private static int CPUblue = 50;
    private static int CPUSelfRed = 255;
    private static int CPUSelfGreen = 170;
    private static int CPUSelfBlue = 51;
    private static Paint paint = new Paint();
    private static final int gray = 100;
    private static final int white = 255;
    private static Paint textPaint = new Paint();
    private static final String percent100 = "100%";
    private static final String percent50 = "50%";
    private static Paint recordPaint = new Paint();
    public static boolean recordFlip = false;
    
    public ProcessView(Context context) {
        super(context);
        MAXSIZE = GTmonitorService.MAXSIZE;
        paintCPU.setColor(Color.argb(100, CPUred, CPUgreen, CPUblue));
        paintSelfCPU.setColor(Color.argb(80, CPUSelfRed, CPUSelfGreen, CPUSelfBlue));
        paint.setColor(Color.argb(50, gray, gray, gray));
        textPaint.setColor(Color.argb(100, white, white, white));
        recordPaint.setColor(Color.RED);
    }
    
    public ProcessView(Context context, AttributeSet attr, int defStyle) {
        super(context, attr, defStyle);
        MAXSIZE = GTmonitorService.MAXSIZE;
        paintCPU.setColor(Color.argb(100, CPUred, CPUgreen, CPUblue));
        paintSelfCPU.setColor(Color.argb(80, CPUSelfRed, CPUSelfGreen, CPUSelfBlue));
        paint.setColor(Color.argb(50, gray, gray, gray));
        textPaint.setColor(Color.argb(100, white, white, white));
        recordPaint.setColor(Color.RED);
    }
    
    public ProcessView(Context context, AttributeSet attr) {
        super(context, attr); 
        MAXSIZE = GTmonitorService.MAXSIZE;
        paintCPU.setColor(Color.argb(100, CPUred, CPUgreen, CPUblue));
        paintSelfCPU.setColor(Color.argb(80, CPUSelfRed, CPUSelfGreen, CPUSelfBlue));
        paint.setColor(Color.argb(50, gray, gray, gray));
        textPaint.setColor(Color.argb(100, white, white, white));
        recordPaint.setColor(Color.RED);
   }
    
    @Override
    public void onDraw(Canvas canvas) {
        canvas.drawColor(Color.BLACK);
        viewWidth = getWidth();
        viewWidth = viewWidth > MAXSIZE ? MAXSIZE : viewWidth;
        viewHeight = getHeight();
        viewBottom = getBottom();
        viewBottom -= (viewBottom - viewHeight);
        graphHeight = viewHeight - textHeight;
        graphBottom = viewBottom - textHeight;

        CPUUsageArray = GTmonitorService.CPULoadArray;
        if (CPUUsageArray == null) return;
        CPUSelfArray = GTmonitorService.CPUSelfLoadArray;
        index = GTmonitorService.index;
        int sid = (index - 1 + MAXSIZE) % MAXSIZE;
        for (int i=sid, c=0; c<viewWidth; ++c) {
            if (GTmonitorService.excludeSelfCPULoad) {
                canvas.drawLine(c, graphBottom, c, 
                        graphBottom - (graphHeight * (CPUUsageArray[i] - CPUSelfArray[i]) / 100), 
                        paintCPU);
            } else {
                canvas.drawLine(c, graphBottom, c, 
                        graphBottom - (graphHeight * CPUUsageArray[i] / 100), 
                        paintCPU);
                canvas.drawLine(c, graphBottom, c, 
                        graphBottom - (graphHeight * CPUSelfArray[i]) / 100, 
                        paintSelfCPU);
            }
            i = (i - 1 + MAXSIZE) % MAXSIZE;
        }
        
        float zero = graphBottom;
        float twentyfive = graphBottom - graphHeight * 0.25f;
        float fifty = graphBottom - graphHeight * 0.5f;
        float seventyfive = graphBottom - graphHeight * 0.75f;
        float hundred = graphBottom - graphHeight;
        canvas.drawLine(0, zero, viewWidth, zero, paint);
        canvas.drawLine(0, twentyfive, viewWidth, twentyfive, paint);
        canvas.drawLine(0, fifty, viewWidth, fifty, paint);
        canvas.drawLine(0, seventyfive, viewWidth, seventyfive, paint);
        canvas.drawLine(0, hundred, viewWidth, hundred, paint);
        canvas.drawText(percent100, 0, graphBottom - graphHeight + 10, textPaint);
        canvas.drawText(percent50, 0, fifty + 5, textPaint);
        canvas.drawText("0min", 0, viewBottom, textPaint);
        canvas.drawText("1.5", 90, viewBottom, textPaint);
        canvas.drawText("3min", 180, viewBottom, textPaint);
        canvas.drawText("4.5", 270, viewBottom, textPaint);
        canvas.drawText("6min", 360, viewBottom, textPaint);
        canvas.drawText("7.5", 450, viewBottom, textPaint);
        if (GTmonitorService.CPULogOn && GraphicsActivity.loggable) {
            canvas.drawCircle(viewWidth - 6, hundred + 6, 6, recordPaint);
        }
    }
}
